####自定义做气泡图
setwd("C:\\Users\\86183\\Desktop\\BP")
x<-read.csv("re.csv",stringsAsFactors = F)
library(ggplot2)

x<-x[x$PValue<0.05,]
x_go=x[,1:5]

xbp=x_go[grep("BP",x_go$Category),]
xcc=x_go[grep("CC",x_go$Category),]
xmf=x_go[grep("MF",x_go$Category),]
xkegg=x_go[grep("KEGG",x_go$Category),]

xbp$Term=gsub(".*\\~","",xbp$Term)#Biological Process
xcc$Term=gsub(".*\\~","",xcc$Term)#Cell Component
xmf$Term=gsub(".*\\~","",xmf$Term)#Molecular Function
xkegg$Term=gsub(".*\\~","",xkegg$Term)#KEGG pathway

make_GO_bubble<-function(go_data,term_namme){
  
  GO_DATA=go_data[order(go_data$Count,decreasing = T),]
  GO_DATA=head(GO_DATA,10)
  
  p=ggplot(GO_DATA,aes(X.,Term))
  bubble=p+ geom_point(aes(size=Count,color=-log10(PValue)))
  
  bubble=bubble+ scale_colour_gradient(low="green",high = "red")
  pr=bubble + theme_test(base_size=10,base_rect_size = 1)
  
  pr=pr+labs(x="Rich factor",y=term_namme,title="Enrichment of DEGs")
  
  return(pr)
}

a=make_GO_bubble(xbp,term_namme = "Biological Process")#HEIGHT 550
b=make_GO_bubble(xcc,term_namme = "Cell Component")
c=make_GO_bubble(xmf,term_namme = "Molecular Function")
d=make_GO_bubble(xkegg,term_namme = "KEGG pathway")
  
ggsave(a,filename = 'Biological Process.png')
ggsave(b,filename = 'Cell Component.png')
ggsave(c,filename = 'Molecular Function.png')
ggsave(d,filename = 'KEGG pathway.png')
getwd()
